﻿using System.Windows;
using System.Windows.Controls;

namespace SDL
{
    using TabViewList = ObservableList<TabView>;

    internal class HeaderPanel : FrameworkElement
    {
        ControlTemplate HeaderTemplate;

        public TabViewList Items
        {
            get { return (TabViewList)GetValue(TabGroup.ItemsProperty); }
            set { SetValue(TabGroup.ItemsProperty, value); }
        }

        protected override System.Collections.IEnumerator LogicalChildren
        {
            get
            {
                return Items.GetEnumerator();
            }
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return Items.Count;
            }
        }

        protected override System.Windows.Media.Visual GetVisualChild(int index)
        {
            return Items[index];
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            Size desiredSize = new Size(0, 0);
            foreach( TabView view in Items )
            {
                view.Measure(availableSize);
                if( view.DesiredSize.Height > desiredSize.Height )
                {
                    desiredSize.Height = view.DesiredSize.Height;
                }
                desiredSize.Width += view.DesiredSize.Width;
            }
            return desiredSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            Point pos = new Point(0, 0);
            foreach( TabView view in Items )
            {
                Size size = view.DesiredSize;
                view.Arrange(new Rect(pos, size));
                pos.X += size.Width;
            }
            return finalSize;
        }
    }
}